//直接更新state多个方法的对象
//将请求回来的数据更新给state
import Vue from 'vue'
import {
    RECEIVE_ADDRESS,
    RECEIVE_CATEGORYS,
    RECEIVE_SHOPS,
    RECEIVE_GOODS,
    RECEIVE_INFO,
    RECEIVE_RATINGS,
    INCREMENT_FOOD_COUNT,
    DECREMENT_FOOD_COUNT,
    CLEAR_CART,
    SEARCH_SHOP,
    RELINK_FOODS,
} from './mutations-types'
export default {
    [RECEIVE_ADDRESS] (state,{address}){
        state.address = address
    },
    [RECEIVE_CATEGORYS] (state,{categorys}){
        state.categorys = categorys
    },
    [RECEIVE_SHOPS] (state,{shops}){
        state.shops = shops
    },
    [RECEIVE_GOODS](state,{goods}){
        state.goods = goods
    },
    [RECEIVE_INFO](state,{info}){
        state.info = info
    },
    [RECEIVE_RATINGS](state,{ratings}){
        state.ratings = ratings
    },
    [INCREMENT_FOOD_COUNT](state,{food}){
        if(!food.count){
            Vue.set(food,'count',1)
            state.cartFoods.push(food)
        }else{
            food.count ++
        }
    },
    [DECREMENT_FOOD_COUNT](state,{food}){
        if(food.count){
            food.count --
            if(food.count === 0){
                state.cartFoods.splice(state.cartFoods.indexOf(food),1)
            }
        }
    },
    [CLEAR_CART](state){
        //清除food中的count
        state.cartFoods.forEach(food => {
            food.count = 0
        });
        //移除购物车中所有购物项
        state.cartFoods = []
    },
    [SEARCH_SHOP](state,{searchShops}){
        state.searchShops = searchShops
    },
    [RELINK_FOODS] (state) {
        
      }
}